package com.markchu.modules.system.service.impl;

import com.markchu.common.data.PageSort;
import com.markchu.common.enums.StatusEnum;
import com.markchu.modules.system.domain.ActivityUserPic;
import com.markchu.modules.system.repository.ActivityUserPicRepository;
import com.markchu.modules.system.service.ActivityUserPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author mark_chu
 * @date 2023/11/23
 */
@Service
public class ActivityUserPicServiceImpl implements ActivityUserPicService {

    @Autowired
    private ActivityUserPicRepository activityUserPicRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public ActivityUserPic getById(Long id) {
        return activityUserPicRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<ActivityUserPic> getPageList(Example<ActivityUserPic> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return activityUserPicRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param activityUserPic 实体对象
     */
    @Override
    public ActivityUserPic save(ActivityUserPic activityUserPic) {
        return activityUserPicRepository.save(activityUserPic);
    }

    @Override
    public void batchSave(Long ajuId,Long acId,Long userId,Long[] fileId){
        List<ActivityUserPic> entities = new ArrayList<>();
        if(Objects.equals(fileId,null)){
           return;
        }
        List<Long> filedIds = Arrays.asList(fileId);
        for(Long fId : filedIds){
            ActivityUserPic activityUserPic = new ActivityUserPic();
            activityUserPic.setAjuId(ajuId);
            activityUserPic.setAc_id(acId);
            activityUserPic.setUser_id(userId);
            activityUserPic.setFile_id(fId);
            entities.add(activityUserPic);
        }
        activityUserPicRepository.saveAllAndFlush(entities);
    }

    @Override
    public List<Map<String,Object>> findAllByacId(Long ajuIds) {
        return activityUserPicRepository.findAllByacId(ajuIds);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return activityUserPicRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}